package com.zhirui.lmwy.common.utils.web;

import java.io.IOException;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

/**
 *@ClassName Iptest
 *@Description TODO
 *@Author longxiaonan@163.com
 *@Date 2021/6/9 0009 11:44
 */
public class Iptest {
    /**
     * 多网卡指定名字类型返回指定InetAddress
     * 输入类型错误则输出全部网卡接口信息
     *
     * @param Name 名字
     * @param type 类型:  4--Inet4Address  6--Inet6Address
     * @return
     */
    public static InetAddress getInetAddress(String Name, int type) {
        try {
            Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces();
            InetAddress addr;
            if (type == 4) {
                while (nifs.hasMoreElements()) {
                    NetworkInterface nif = nifs.nextElement();
                    Enumeration<InetAddress> addresses = nif.getInetAddresses();
                    while (addresses.hasMoreElements()) {
                        addr = addresses.nextElement();
                        if (nif.getName().equals(Name) && addr instanceof Inet4Address) {
                            return addr;
                        }
                    }
                }
            } else if (type == 6) {
                while (nifs.hasMoreElements()) {
                    NetworkInterface nif = nifs.nextElement();
                    Enumeration<InetAddress> addresses = nif.getInetAddresses();
                    while (addresses.hasMoreElements()) {
                        addr = addresses.nextElement();
                        if (nif.getName().equals(Name) && addr instanceof Inet6Address) {
                            return addr;
                        }
                    }
                }
            } else {
                System.out.println("类型指定错误,将输出全部网卡接口信息");
                System.out.println();
                while (nifs.hasMoreElements()) {
                    NetworkInterface nif = nifs.nextElement();
                    Enumeration<InetAddress> addresses = nif.getInetAddresses();
                    while (addresses.hasMoreElements()) {
                        addr = addresses.nextElement();
                        System.out.println("网卡接口名称：" + nif.getName());
                        System.out.println("网卡接口地址：" + addr.getHostAddress());
                        System.out.println();
                    }
                }
                return null;
            }

        } catch (Exception e) {
            System.out.println("获取Socket失败");
        }
        return null;
    }

    public static void main(String[] args) throws IOException {
//        InetAddress inetAddress=getInetAddress("无线局域网适配器 WLAN", 4);

        InetAddress inetAddress=getInetAddress("wlan0", 4);
        System.out.println(inetAddress);
    }

}
